import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';
import 'package:sui_shou_pai/network/http_config.dart';
import 'package:sui_shou_pai/network/http_request.dart';
import 'package:sui_shou_pai/news/data/comment_list_item_data.dart';

import 'comment_list.dart';

class AnnouncementCommentList extends StatefulWidget {
  final String announcementId;

  Function refreshComment;

  AnnouncementCommentListState _state = AnnouncementCommentListState();

  AnnouncementCommentList({Key key, this.announcementId}) : super(key: key);

  @override
  AnnouncementCommentListState createState() {
    refreshComment = (isRefresh) {
      if (isRefresh) {
        _state.initData();
      }
    };
    return _state;
  }
}

class AnnouncementCommentListState extends State<AnnouncementCommentList> {
  List<dynamic> tempList = [];
  List<CommentListItemData> commentList = [];

  @override
  void initState() {
    super.initState();
    Future.delayed(
        Duration.zero,
        () => setState(() {
              initData();
            }));
  }

  initData() async {
    commentList.clear();
    HttpRequest.request(
            "$BASE_URL/announcement-comment/announcementCommentList",
            method: "GET",
            params: {"announcementId": widget.announcementId},
            token: await StorageUtil.getStringItem("token"))
        .then((value) {
      setState(() {
        tempList = json.decode(value.toString())['result'];
      });
      for (int i = 0; i < tempList.length; i++) {
        commentList.add(CommentListItemData.fromMap(tempList[i]));
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return initCommentList(commentList, context);
  }
}
